package com.woniuxy.ticket.ticketmovie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.ticket.ticketmovie.entity.Movie;
import com.woniuxy.ticket.ticketmovie.entity.ResponseResult;
import com.woniuxy.ticket.ticketmovie.entity.dto.MovieDTO;
import com.woniuxy.ticket.ticketmovie.entity.dto.MovieQO;
import com.woniuxy.ticket.ticketmovie.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "电影管理接口")
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Resource
    private MovieService movieService;

    @ApiOperation("根据类型id分页查询电影")
    @GetMapping("/findMovieByTypeId")
    public ResponseResult<List<Movie>> findMovieByTypeId(Page<Movie> page, int id){
        return new ResponseResult<List<Movie>>().success(movieService.findMovieByTypeId(page, id));
    }

    @ApiOperation("根据电影id查询电影信息")
    @RequestMapping(value = "/findMovieById/{id}",method = RequestMethod.GET)
    public ResponseResult<Movie> findMovieById(@PathVariable int id){
        return new ResponseResult<Movie>().success(movieService.getById(id));
    }

    @ApiOperation("根据电影以及类型id添加电影")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody MovieDTO movieDTO) {
        movieService.add(movieDTO.getMovie(), movieDTO.getTypeIds());
        return new ResponseResult().success();
    }

    @GetMapping("/list")
    public ResponseResult<Page<Movie>> list(@RequestBody MovieQO movieQO){
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        if(movieQO.getName() != null && movieQO.getName().trim().length() != 0){
            queryWrapper.like("name",movieQO.getName());
        }
        if(movieQO.getDirector() != null && movieQO.getDirector().trim().length() != 0){
            queryWrapper.eq("director",movieQO.getDirector());
        }
        Page<Movie> page = new Page<>(movieQO.getP() == null ? 1:movieQO.getP(),2);

        page = movieService.page(page, queryWrapper);

        return new ResponseResult<Page<Movie>>().success(page);
    }
}
